package com.sky.happygos.base;

import android.app.Activity;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by qqqqqqq on 2017/4/23.
 */

public class YActivityManage {

    private static YActivityManage sInstance = new YActivityManage();
    private WeakReference<Activity> sCurrentActivityWeakRef;

    private List<WeakReference<Activity>> activitys;


    private YActivityManage() {
        activitys = new ArrayList<WeakReference<Activity>>();
    }

    public static YActivityManage getInstance() {
        return sInstance;
    }

    public Activity getCurrentActivity() {
        Activity currentActivity = null;
        if (sCurrentActivityWeakRef != null) {
            currentActivity = sCurrentActivityWeakRef.get();
        }
        return currentActivity;
    }

    public void setCurrentActivity(Activity activity) {
        sCurrentActivityWeakRef = new WeakReference<Activity>(activity);
    }

    public void pushActivity(Activity activity){
        activitys.add(new WeakReference<Activity>(activity));
    }

    public void popActivity(Activity activity){
        activitys.remove(activity);
    }

    public void finishAll(){
        for (WeakReference<Activity> weak: activitys) {
            if(weak != null){
                Activity activity = weak.get();
                if(activity != null) {
                    activity.finish();
                }
            }
        }
        activitys = new ArrayList<WeakReference<Activity>>();
        sCurrentActivityWeakRef = null;

    }
}
